﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Wasion.HHU.Data.Models;
using Wasion.HHU.Data.Repositories;
using SQLite.Net;
using System;
using System.Linq.Expressions;

namespace Wasion.HHU.Data.Repositories
{
    public class IclassDbRepository : BaseDbRepository
    {
        public IclassDbRepository() : base()
        {

        }
        public IclassDbRepository(SQLiteConnection connection)
            : base(connection)
        {
        }

        public Task<List<Iclass>> GetAllList()
        {
            List<Iclass> iclasss;

            lock (databaseLock)
            {
                iclasss = DbConnection.Table<Iclass>().
                    OrderBy(x => x.ClassID).
                    ToList();
            }
            return Task.FromResult(iclasss);
        }

        public Task<List<Iclass>> GetList(Expression<Func<Iclass, bool>> filter)
        {
            List<Iclass> iclasss;

            lock (databaseLock)
            {
                iclasss = DbConnection.Table<Iclass>().
                    OrderByDescending(x => x.ClassID).
                    Where(filter).
                    ToList();
            }
            return Task.FromResult(iclasss);
        }

        public void Insert(Iclass iclass)
        {
            lock (databaseLock)
            {
                DbConnection.CreateTable<Iclass>();
                DbConnection.Insert(iclass);
            }
        }

        public void Update(Iclass iclass)
        {
            lock (databaseLock)
            {
                DbConnection.Update(iclass);
            }
        }

        public Iclass Find(object primaryKey)
        {
            Iclass iclass;

            lock (databaseLock)
            {
                iclass = DbConnection.Find<Iclass>(primaryKey);
            }
            return iclass;
        }

        public void Delete(object primaryKey)
        {
            lock (databaseLock)
            {
                DbConnection.Delete<Iclass>(primaryKey);
            }
        }
    }
}
